<?php 
/**
 * This file is part of DomFg.
 * 
 * DomFg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomFg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomFg.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @author Oliver Leisalu, Inditel Meedia
* @package DomFg
* @subpackage Grid
*/
class DomFgGrid extends DomFgComponent {
	
	protected $source;
	
	protected $name;
	
	protected $fields = array();
	
		/**
	 * <code>
//Sample handler function
function myRowHandler( &$row, $i, $customArg1, $customArg2) {
	return $row['name']; // if $row is array.
}
//Definition:
array(
	'rowHandler' => 'myHandler',
	'key' => NULL,
	'rowHandlerArgs' => array($customArg1, $customArg2)
);

//Sample handler function
function myRowHandler( $name, &$row, $i, $customArg1, $customArg2) {
	return $name;
}
//Definition:
array(
	'rowHandler' => 'myHandler',
	'key' => name,
	'rowHandlerArgs' => array($customArg1, $customArg2)
);
	 * </code>
	 *
	 * @var String
	 */
	protected $rowHandler;
	
	protected $rowHandlerArgs = array();
	
	protected $link;
	
	/**
	 * If DomFgPost::getInstance()->{$pagerResetPostVar} is true then pager is resetted to page nr 0.
	 * Very useful for grids with search function. Helps to avoid situation where real result has for example 10 rows but you are on page 32. 
	 * For default you would see page with no results, but using this you are resetted to page 0.
	 * @var String
	 */
	protected $pagerResetPostVar;
	
	const SESSION_VAR = 'domfggrid';
	
	public function __construct() {
		
	}
	
	public function source( DomFgDataSource $source ) {
		$this->source = $source;
		return $this;
	}
	
	public function name( $name ) {
		if( $this->name !== NULL ) DomFgPost::unregisterInput( $this->name );
		$this->name = $name;
		if( $name != NULL ) DomFgPost::registerInput( $this );	
		return $this;
	}
	
	public function link( $link ) {
		$this->link = $link;
		return $this;
	}
	
	public function rowHandler( $handler ) {
		$this->rowHandler = $handler;
		return $handler;
	}
	
	public function rowHandlerArgs( $rowHandlerArgs ) {
		$this->rowHandlerArgs = $rowHandlerArgs;
		return $rowHandlerArgs;
	}
	
	protected function getHeaderFields() {
		$fields = array();
		$fieldSkip = 0;
		foreach( $this->fields as $field ) {
			if( $fieldSkip <= 1) {
				$fields[] = $field;
				$fieldSkip = $field->colspan;
			} else {
				$fieldSkip--;
			}
		}
		return $fields;
	}
	
	protected function getData() {
		$data = array();
		if( $this->source ) {
			foreach( $this->source->getData($this->getPageNr()*$this->source->perPage) as $i => $row ) {
				if( $this->rowHandler )
					$row = call_user_func_array( $this->rowHandler, array_merge( array($row), $this->rowHandlerArgs) );
				
				$newRow = array();
				foreach( $this->fields as $field )
					$newRow[] = $field->getValue( $row, $i );

				$data[] = $newRow;
			}
		}
		//debug($data);
		return $data;
	}
	
	protected function getPager() {
		
		if( $this->source ) 
			$pager = new DomPager( $this->source->getTotal(), $this->source->perPage, $this->link );
		else
			$pager = new DomPager(1,1, $this->link );
		
		$pager->setVar( $this->getPageNrVar() );
		$pager->setCurrentPage( $this->getPageNr() );
		return $pager;
	}
	
	public function getOrder() {
		return isset($_GET[$this->getOrderVar()]) ? $_GET[$this->getOrderVar()] : $_SESSION[self::SESSION_VAR][$this->name]['order'];
	}
	
	public function getOrderDirection() {
		return (isset($_GET[$this->getOrderDirectionVar()]) ? $_GET[$this->getOrderDirectionVar()] : $_SESSION[self::SESSION_VAR][$this->name]['orderDir']) == 1 ? 1 : 0;
	}
	
	public function getPageNr() {
		if( $this->pagerResetPostVar !== NULL ) {
			$post = DomFgPost::getInstance();
			if( $post->{$this->pagerResetPostVar} )
				return 0;
		}
		return (int)(isset($_GET[$this->getPageNrVar()]) ? $_GET[$this->getPageNrVar()] : $_SESSION[self::SESSION_VAR][$this->name]['pg']);
	}
	
	public function pagerResetPostVar( $pagerResetPostVar ) {
		$this->pagerResetPostVar = $pagerResetPostVar;
	}
	
	
	
	public function render() {
		$renderer = $this->getRenderer();
		
		$post = DomFgPost::getInstance();
		foreach( $this->fields as $field ) {
			if( $field->orderByField == $post->{$this->name}['orderByField'] && $post->{$this->name}['orderByDirection'] ) {
				$field->orderByDirection( $post->{$this->name}['orderByDirection'] == 'ASC' ? 'DESC' : 'ASC' );
			}
		}
		
		$renderer->assign('currentOrderByField', $post->{$this->name}['orderByField'] );
		$renderer->assign('currentOrderByDirection', $post->{$this->name}['orderByDirection'] );
		
		$renderer->assign('grid', $this );
		$renderer->assign('nrOfColumns', count($this->fields) );
		$renderer->assign('data', $this->getData() );
		$renderer->assign('headerFields', $this->getHeaderFields() );
		$renderer->assign('fields', $this->fields );
		$renderer->assign('pager', $this->getPager() );

		$renderer->assign('url', $this->link );
		
		/*
		if( count($this->source->searchKeys) > 0 ) {
			$renderer->assign( 'allowSearch', true);
			$input = new DomFgTextField();
			$input->name('searchWord')->value('a');	
			$renderer->assign('field', $input->render() );
			$button = new DomFgButton();
			$button->name('doSearch')->title('Otsi');
			$renderer->assign('button', $button->render() );
		}
		*/
		
		return $renderer->fetch('grid.html');
	}
	
	public function addField( DomFgGridField $field ) {
		$this->fields[] = $field;
	}
	
	public function getDefaultChild() {
		return 'GridField';
	}
	
	public function getPageNrVar() {
		return $this->name.($this->name ? 'Pg' : 'pg');
	}
	
	public static function parsePost( $name, &$value ) {
		return array(
			'orderByField' => addslashes($value['orderByField']),
			'orderByDirection' => strtolower($value['orderByDirection']) == 'asc' ? 'ASC' : 'DESC',
		);
	}
	
	public function __destruct() {
		//$_SESSION[self::SESSION_VAR][$this->name]['order'] = $this->getOrder();
		//$_SESSION[self::SESSION_VAR][$this->name]['orderDir'] = $this->getOrderDirection();
		//$_SESSION[self::SESSION_VAR][$this->name]['pg'] = $this->getPageNr();
	}
	

}
?>
